package com.aurora.source.source.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.aurora.source.R;
import com.aurora.source.base.BaseActivity;
import com.aurora.source.entity.SysPermissionEntity;
import com.aurora.source.entity.UserEntity;
import com.aurora.source.event.EventData;
import com.aurora.source.home.activity.MainActivity;
import com.aurora.source.source.bean.UserInfoBean;
import com.aurora.source.source.presenter.LoginSourcePresenter;
import com.aurora.source.source.view.ILoginSourceView;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.util.SPUtils;
import com.aurora.source.util.StatusBarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: ww
 * @description: 登录
 * @date: 2018/8/10 11:12
 */
public class LoginActivity extends BaseActivity<LoginSourcePresenter> implements ILoginSourceView {

    @BindView(R.id.tv_upper)
    CheckBox tvUpper;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    public LoginSourcePresenter attach() {
        return new LoginSourcePresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        EventBus.getDefault().register(this);
        boolean isRememberPass = (boolean) SPUtils.get(this, SPUtils.IS_SAVE_PASS, false);
        tvUpper.setChecked(isRememberPass);
        //恢复用户名密码
        List<UserEntity> userEntities = GreenDaoHelper.getDaoSession().getUserEntityDao().loadAll();
        if (userEntities.size() != 0) {
            UserEntity userEntity = userEntities.get(0);
            etNumber.setText(userEntity.getUserAccount());
            if (isRememberPass) {
                etPassword.setText(userEntity.getUserPwd());
            }
        }


    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        StatusBarUtils.setWindowStatusBarColor(LoginActivity.this, R.color.source);
        StatusBarUtils.StatusBarDarkMode(getWindow());
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    public void doBusiness() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                Map<String, String> params = new HashMap<>();
                params.put("userAccount", etNumber.getText().toString());
                params.put("password", etPassword.getText().toString());
                // gotoActivity(MainActivity.class,false);
                basePresenter.loginSource(params);
                break;
        }
    }


    //接收退出登录操作发过来的消息  执行退出应用
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void helloEventBus(String message) {
        if (message.equals(EventData.logOut)) {
            SPUtils.put(this, SPUtils.IS_SAVE_PASS, false);
            GreenDaoHelper.getDaoSession().getUserEntityDao().deleteAll();
            etPassword.setText("");
            etNumber.setText("");
            tvUpper.setChecked(false);
        }
    }

    @Override
    public void onLoginSourceSuccess(UserInfoBean data) {
        //是否保存密码
        SPUtils.put(this, SPUtils.IS_SAVE_PASS, tvUpper.isChecked());//账户id
        //userId放sp
        SPUtils.put(this, SPUtils.USER_ID, data.getUserInfoDTO().getUserId());//账户id
        //用户信息及权限放数据库
        UserEntity userEntity = data.getUserInfoDTO();
        userEntity.setUserPwd(etPassword.getText().toString());
        //插入用户信息
        GreenDaoHelper.getDaoSession().getUserEntityDao().deleteAll();
        GreenDaoHelper.getDaoSession().getUserEntityDao().insert(userEntity);
        //初始化权限信息
        GreenDaoHelper.getDaoSession().getSysPermissionEntityDao().deleteAll();
        SysPermissionEntity sysPermissionEntity = new SysPermissionEntity();
        sysPermissionEntity.setUserId(userEntity.getUserId());
        List<String> sysRoleList = userEntity.getSysRoleList();//权限列表
        if (sysRoleList != null && sysRoleList.size() > 0) {
            //ADD_USER,ADD_BATCH,ADD_LOGISTICS,UPDATE_COMPANY,ADD_PRODUCT
            if (sysRoleList.contains("ADD_USER")) { //添加子账号
                sysPermissionEntity.setAddUser(true);
            }
            if (sysRoleList.contains("ADD_BATCH")) { //添加批次
                sysPermissionEntity.setAddBatch(true);
            }
            if (sysRoleList.contains("ADD_LOGISTICS")) { //添加物流
                sysPermissionEntity.setAddLogistics(true);
            }
            if (sysRoleList.contains("UPDATE_COMPANY")) { //修改公司
                sysPermissionEntity.setUpdateCompany(true);
            }
            if (sysRoleList.contains("ADD_PRODUCT")) { //添加商品
                sysPermissionEntity.setAddProduct(true);
            }
        }
        //插入权限信息
        GreenDaoHelper.getDaoSession().getSysPermissionEntityDao().insert(sysPermissionEntity);

        Bundle bundle = new Bundle();
        bundle.putStringArrayList("bannerList", (ArrayList<String>) userEntity.getBannerPathList());
        gotoActivity(MainActivity.class, false, bundle);
    }

    @Override
    public void onLoginSourceFail(String msg) {
        showToast(msg);
    }

}
